//index.js
const app = getApp()
const API = require('../../utils/api.js')
const tools = require('../../utils/util.js')
Page({
	data: {
		searchResult: null,
		result: false,
		article:[], //文章列表
		message:"",
		background:[
			"https://img2.baidu.com/it/u=3674359777,3425804912&fm=26&fmt=auto&gp=0.jpg",
			"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn20%2F480%2Fw640h640%2F20180507%2F97d0-hacuuvu6297343.jpg&refer=http%3A%2F%2Fn.sinaimg.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629620843&t=20bb097b6afa14a167caecc11a160a70",
			"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic4.zhimg.com%2F50%2Fv2-49b10a700732cfb7c9727c546e12492b_hd.jpg&refer=http%3A%2F%2Fpic4.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629622864&t=3344fa135ebe3f4b3b2e0cba5662bce7",
			"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202004%2F22%2F20200422185508_Vrt2G.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629620776&t=b860ddf2f358a57d083db11f9dd7cbf2"
		]
	},
	onLoad: function (options) {
		console.log("------------options",options)
		// 使用 Mock
		let data = {};
		let that = this;
		this.getArticleList()
		this.getCommentNum()
	
	},
	onShow:function(){
	},
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function (res) {
			return{title:'文章列表'}
	},
	onShareTimeline:function(res){
		return{
			title:'转发到朋友圈',
			
		}
	},
	// 自定义方法
	//事件处理函数

	/**
	 * 文章详情
	 */
	showArticle: function (e) {
		console.log('---------index',e)
		console.log('------0',e.currentTarget.dataset.id)		//文章id
		wx.navigateTo({
			url: '/pages/articleContent/articleContent?id=' + e.currentTarget.dataset.id,
		})
	},

	
  //获取文章列表内容
  getArticleList(){
    let that = this
    wx.request({
      url: 'https://xiaxueruo.com/api/article/list/',
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',//get或者post
      success: function (res) {
				app.globalData.articleList = res.data.data.articleList
				console.log('---app.globalData.articleList',app.globalData.articleList)//接口返回值
				for(let item of app.globalData.articleList){
					console.log('-------------tools',tools.formatTime(item.add_time))
					item.add_time = tools.formatTime(item.add_time)
				}
        console.log(res)//接口返回值
        console.log('1111111',res.data.data.articleList)
        // console.log('22222',res.data.data.articleData[0].article_title)
        that.setData({
					article: res.data.data.articleList,
					articles: app.globalData.articleList
				})
      }
		})
	
	},
	
	getCommentNum(){
		let that = this
		wx.request({
			url: 'https://xiaxueruo.com/api/comment/index',
			success: function(res){

			}
		})
	}

	// 搜索
	// searchArtile(e) {
	// 	// console.log(e)
	// 	let key = e.detail.value;
	// 	if(key == ' ' || key == null || key.length == 0){
	// 		this.setData({
	// 			result: false
	// 		})
	// 		return
	// 	}else{
	// 		let list = this.data.articles;
	// 		for (let i = 0; i < list.length; i++) {
	// 			let a = key;
	// 			let b = list[i].title;
	// 			this.setData({
	// 				result: true
	// 			})
	// 			if (b.search(a) != -1) {
	// 				list[i].resultShow = true
	// 			} else {
	// 				list[i].resultShow = false
	// 			}
	// 		}
	// 		this.setData({
	// 			searchResult: list
	// 		})
	// 		// console.log(this.data.searchResult)
	// 	}	
	// }
})
